﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.DTO;
using QLBH.DataModel;
using QLBH.Model.Controller;
using Module.Category.Model;

namespace Module.Category.BL
{
    public class ProductManagementBL
    {
        private static ProductManagementBL instance;

        public static ProductManagementBL Instance
        {
            get 
            {
                if (instance==null)
                {
                    instance = new ProductManagementBL();
                }
                return instance; 
            }
            
        }
        public void AddProduct(HangHoaDTO newProduct)
        {
            try
            {
                ProductController.Instance.Add(newProduct.ToEntity());
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }

        }
        public IList<ProductModel> GetAllProducts()
        {
            try
            {
                var result=ProductController.Instance.GetAll();
                if (result==null)
                {
                    return null;
                }
                List<HangHoaDTO> listproductsdto= result.ToDTOs().ToList();
                List<ProductModel> listProducts = new List<ProductModel>();
                foreach (HangHoaDTO product in listproductsdto)
                {
                    String dvt = CalculateUnitManagementBL.Instance.GetCalculateUnit(product.DVTId).TenDV;
                    ProductModel productModel = new ProductModel
                        (
                            product.Id,
                            product.TenMH,
                            product.NgaySX.ToShortDateString(),
                            product.HanSD.ToShortDateString(),
                            dvt,
                            product.GiaNhap,
                            product.GiaBan,
                            product.SLTon
                        );
                    listProducts.Add(productModel);
                }
                return listProducts;
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
                    
        }       
    }
}
